﻿using Oracle.ManagedDataAccess.Client;
using Server.Constant;
using Server.Enum;
using Server.OptExp;
using Server.Framework.Message;
using Server.Framework.Processor;
using Server.Framework.Service;
using Server.Framework.Socket;
using Server.Message.Request;
using Server.Message.Response;
using Server.Model;
using System.Data.Common;
using System;

namespace Server.Processor
{
    /// <summary>
    /// Author: Leil
    /// Description: 新增或编辑组织
    /// CreateTime: 2020/5/13 10:12:49
    /// </summary>
    [ProcessorAttr(MessageId.ADD_EDIT_ORG)]
    class AddEditOrgProcessor : IProcessor
    {

        public void Process(ProtobufAppSession session, RequestBaseMessage message)
        {
            if (session.User == null)
            {
                Console.WriteLine("用户未登录");
                return;
            }


            //session.User = new UserDao()
            //{
            //    ID = 100,
            //    LoginName = "Admin"
            //};

            if (message is AddEditOrgReq msg)
            {
                OperationResp resp = new OperationResp(MessageId.OPT_RESP);
                if (msg.Org == null)
                {
                    resp.Code = 500;
                    resp.OptType = (int)OptType.ADD_EDIT_ORG;
                    resp.Content = "组织不能为空";
                    session.Send(resp);
                    return;
                } else
                {
                    try
                    {
                        if (msg.Org.ID != 0)
                        {
                            EditOrg(msg.Org, session);
                        }
                        else
                        {
                            AddOrg(msg.Org, session);
                        }
                    }
                    catch (CommonOptException e)
                    {
                        resp.Code = 500;
                        resp.OptType = (int)OptType.ADD_EDIT_ORG;
                        resp.Content = e.Message;
                        session.Send(resp);
                    }
                }
            }
        }


        /// <summary>
        /// 新增组织
        /// </summary>
        /// <param name="permission">组织</param>
        /// <param name="resp">消息</param>
        private void AddOrg(OrgDao org, ProtobufAppSession session)
        {
            if (string.IsNullOrEmpty(org.OrgName)) {
                throw new CommonOptException("组织名称不能为空");
            }
            string sql = $@"INSERT INTO SYS_ORG 
( ID, PARENT_ID, ORG_NAME, STATUS, SORT, CREATOR, CREATE_TIME )
VALUES
(seq_sys_org.NEXTVAL, {org.ParentId}, '{org.OrgName}', {org.Status}, {org.Sort}, '{session.User.LoginName}', SYSDATE)";
            
            DBService.Instance().ExecuteNonQuery(sql, res => {
                OperationResp resp = new OperationResp(MessageId.OPT_RESP);
                if (res > 0)
                {
                    resp.Code = 200;
                    resp.OptType = (int)OptType.ADD_EDIT_ORG;
                    resp.Content = "新增成功";
                    session.Send(resp);
                } else
                {
                    resp.Code = 500;
                    resp.OptType = (int)OptType.ADD_EDIT_ORG;
                    resp.Content = "新增失败";
                    session.Send(resp);
                }
            });
        }

        /// <summary>
        /// 编辑组织
        /// </summary>
        /// <param name="permission">组织</param>
        /// <param name="resp">消息</param>
        private void EditOrg(OrgDao org, ProtobufAppSession session)
        {
            string sql = $@"UPDATE SYS_ORG 
SET 
PARENT_ID = {org.ParentId},
ORG_NAME = '{org.OrgName}',
STATUS = {org.Status},
SORT = {org.Sort},
UPDATOR = '{session.User.LoginName}',
UPDATE_TIME = SYSDATE
WHERE ID = {org.ID}";

            DBService.Instance().ExecuteNonQuery(sql, res => {
                OperationResp resp = new OperationResp(MessageId.OPT_RESP);
                if (res > 0)
                {
                    resp.Code = 200;
                    resp.OptType = (int)OptType.ADD_EDIT_ORG;
                    resp.Content = "更新成功";
                    session.Send(resp);
                }
                else
                {
                    resp.Code = 500;
                    resp.OptType = (int)OptType.ADD_EDIT_ORG;
                    resp.Content = "更新失败";
                    session.Send(resp);
                }
            });
        }


    }
}
